import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__), '../'))
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib
matplotlib.rc("font",family='FangSong')
from  utils import column_letter_to_index
from sklearn.ensemble import RandomForestRegressor

# D:\software\miniconda\python.exe
data_path = '../../data/raw_data/334份 按选项序号 汇总变量后.xlsx'
df = pd.read_excel(data_path)  

y_index_list = [column_letter_to_index(each) for each in ['AT','CG','CH','CI']]
# print('drop 之前')

#先找到因变量
df_y = df[df.columns[y_index_list]]
# 去除自变量
df_x = df.drop(df.columns[y_index_list], axis=1) # 自变量
# 去除第一列
df_x = df_x.drop(df.columns[0], axis=1)


for i in range(df_y.shape[1]):
    print('--------------{}--------------'.format(df_y.columns[i]))
    this_y = df_y.iloc[:,i]
    this_x = df_x

    # 创建随机森林回归模型
    rf = RandomForestRegressor()

    # 拟合模型
    rf.fit(this_x, this_y)

    # 获取特征重要性
    feature_importances = rf.feature_importances_

    importance_df = pd.DataFrame({'Feature': this_x.columns, 'Importance': feature_importances})
    # # 打印特征重要性
    # for i, importance in enumerate(feature_importances):
    #     print(f'特征{i+1}的重要性：{importance}')
    plt.pie(importance_df['Importance'], labels=importance_df['Feature'], autopct='%1.1f%%')
    plt.axis('equal')
    plt.title('Feature Importance')
    plt.show()
        